"use strict";
const { contextBridge, ipcRenderer } = require('electron')

// 调试信息
console.log('Preload script loaded');

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron
})

// 添加SerialPort相关功能
contextBridge.exposeInMainWorld('serialAPI', {
  listPorts: () => ipcRenderer.invoke('serial:list-ports'),
  connect: (path, options) => ipcRenderer.invoke('serial:connect', path, options),
  disconnect: () => ipcRenderer.invoke('serial:disconnect'),
  sendData: (data) => ipcRenderer.invoke('serial:send-data', data),
  onReceiveData: (callback) => {
    ipcRenderer.removeAllListeners('serial:receive-data');
    ipcRenderer.on('serial:receive-data', (event, data) => callback(data));
  },
  onReceiveError: (callback) => {
    ipcRenderer.removeAllListeners('serial:receive-error');
    ipcRenderer.on('serial:receive-error', (event, error) => callback(error));
  }
})

// 调试信息：确认API已暴露
console.log('serialAPI exposed');
